package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageReturnDetailActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageReturnOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageReturnOverviewBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.serialNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class ReturnOutStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageReturnOverviewBean.DataBean.MaterielListBean> mReturnOutStorageItemList;
    private LayoutInflater inflater;
    private OutStorageReturnOverviewActivity.OutStorageReturnOverviewListener mOutStorageReturnListener;
    private String mMaterielCode, mPurchaseNumber, mSupplier, mSupplierCode, mCreateDate, mOrderType, mPurchaseOrganization, mPurchaseGroup, mContractCode, mContractDescription;

    public ReturnOutStorageItemListAdapter(Context context, OutStorageReturnOverviewBean.DataBean data, List<OutStorageReturnOverviewBean.DataBean.MaterielListBean> outStorageReturnItemList, String materielCode, OutStorageReturnOverviewActivity.OutStorageReturnOverviewListener outStorageReturnOverviewListener) {
        mContext = context;
        mPurchaseNumber = data.getPurchaseNumber();
        mSupplier = data.getSupplier();
        mSupplierCode = data.getSupplierCode();
        mCreateDate = data.getCreateDate();
        mOrderType = data.getOrderType();
        mPurchaseOrganization = data.getPurchaseOrganizationCode() + " - " + data.getPurchaseOrganizationValue();
        mPurchaseGroup = data.getPurchaseGroupCode() + " - " + data.getPurchaseGroupValue();
        mContractCode = data.getContractCode();
        mContractDescription = data.getContractDescription();
        mReturnOutStorageItemList = outStorageReturnItemList;
        mOutStorageReturnListener = outStorageReturnOverviewListener;
        inflater = LayoutInflater.from(context);
        mMaterielCode = materielCode;
    }

    @Override
    public int getCount() {
        return mReturnOutStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReturnOutStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_return_item_list_first_item, null);
            TextView purchaseNumber = (TextView) convertView.findViewById(R.id.purchaseNumber);
            TextView supplier = (TextView) convertView.findViewById(R.id.supplier);
            TextView createDate = (TextView) convertView.findViewById(R.id.createDate);
            TextView orderType = (TextView) convertView.findViewById(R.id.orderType);
            TextView purchaseOrganization = (TextView) convertView.findViewById(R.id.purchaseOrganization);
            TextView purchaseGroup = (TextView) convertView.findViewById(R.id.purchaseGroup);
            TextView contractCode = (TextView) convertView.findViewById(R.id.contractCode);
            TextView contractDescription = (TextView) convertView.findViewById(R.id.contractDescription);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            purchaseNumber.setText(mPurchaseNumber);
            supplier.setText(mSupplier);
            createDate.setText(mCreateDate);
            orderType.setText(mOrderType);
            purchaseOrganization.setText(mPurchaseOrganization);
            purchaseGroup.setText(mPurchaseGroup);
            contractCode.setText(mContractCode);
            contractDescription.setText(mContractDescription);
            materielCode.setText(mMaterielCode);
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, materielCode);
                    mOutStorageReturnListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageReturnListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            OutStorageReturnOverviewBean.DataBean.MaterielListBean returnOutstorageItem = mReturnOutStorageItemList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, returnOutstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.canSendNumber = (TextView) view.findViewById(R.id.canSendNumber);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStorageReturnOverviewBean.DataBean.MaterielListBean returnOutStorageItem) {
        viewHolder.serialNumber.setText(returnOutStorageItem.getSerial());
        viewHolder.materielCode.setText(returnOutStorageItem.getMaterielCode());
        viewHolder.materielDescribe.setText(returnOutStorageItem.getMaterielDescription());
        viewHolder.canSendNumber.setText("可发" + returnOutStorageItem.getCanSendNumber() + returnOutStorageItem.getUnit());
        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(returnOutStorageItem.getActualSendNumber()) ? "填写数量" : ("实发" + returnOutStorageItem.getActualSendNumber() + returnOutStorageItem.getUnit()));

        if (TextUtils.isEmpty(returnOutStorageItem.getActualSendNumber())) {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        }

        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStorageReturnDetailActivity.class);
                String serial = viewHolder.serialNumber.getText().toString();
                intent.putExtra(OutStorageExtra.SERIAL, serial);
                intent.putExtra(OutStorageExtra.SUPPLIER_CODE, mSupplierCode);
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, canSendNumber, outStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
